<template>
  <BasicModal
    v-bind="$attrs"
    @register="registerModal"
    destroyOnClose
    :title="title"
    :width="800"
    @ok="handleSubmit"
  >
    <!-- 表格区域 -->
    <BasicTable @register="registerTable">
      <!-- 表格标题插槽 -->
      <template #tableTitle>
        <div>审核反馈列表</div>
      </template>

      <!-- 自定义单元格插槽 -->
      <template #bodyCell="{ column, record }">
        <!-- 操作列按钮 -->
        <template v-if="column.dataIndex === 'action'">
          <a-button size="small" type="primary" @click="handleReceive(record)">
            接收
          </a-button>
        </template>
      </template>
    </BasicTable>
  </BasicModal>
</template>

<script lang="ts" setup>
import { ref, unref } from 'vue';
import { BasicModal, useModalInner } from '/@/components/Modal';
import { BasicTable, useTable } from '/@/components/Table';
import { useMessage } from '/@/hooks/web/useMessage';
import { shenhefankuiList, shenhefankui } from '../Yuedupeihebumenbiao.api';

const { createMessage } = useMessage();
const emit = defineEmits(['success']);
const currentRecord = ref<any>(null);

// 弹窗标题
const title = '审核反馈';

// 表格列配置（不使用 render）
const columns = [
  {
    title: '配合部门',
    align: 'center',
    dataIndex: 'peihebumen_dictText',
  },
  {
    title: '完成时间',
    align: 'center',
    dataIndex: 'wanchengshijian',
  },
  {
    title: '操作',
    align: 'center',
    dataIndex: 'action',
    width: 200,
  },
];

// 注册表格
const [registerTable, { reload }] = useTable({
  api: shenhefankuiList,//必须有，才能加载出来表格
  columns,
  showActionButtonGroup: false,

  //  请求前拼接父组件传入的完整 record 参数
  beforeFetch: (params) => {
    const record = unref(currentRecord);
    return {
      ...params,
      ...record, // 把父组件传入的完整数据对象都传给后端
    };
  },
});

// 注册弹窗（接收父组件传入的参数）
const [registerModal] = useModalInner((data) => {
  // 保存完整 record
  currentRecord.value = { ...data.record };
  console.log('父组件传入完整数据:', currentRecord.value);
  // 每次打开弹窗，加载表格
  reload();
});


// 点击“接收”按钮
const handleReceive = async (record) => {
  try {
    await shenhefankui({ id: record.id, zhuangtai: '已接收' });
    createMessage.success('接收成功');
    reload();
  } catch (error) {
    createMessage.error('操作失败');
  }
};

// 弹窗确认事件
const handleSubmit = () => {
  emit('success');
};
</script>
